<?php

// +----------------------------------------------------------------------
// | H1CMS © OpenSource CMS
// +----------------------------------------------------------------------
// | Copyright (c) 2014-2016 http://www.h1cms.com All rights reserved.
// | Copyright (c) 2014-2016 嘉兴领格信息技术有限公司，并保留所有权利。
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Allen <allen@lg4.cn>
// +----------------------------------------------------------------------

require '../autoload.php';

use h1cms\model\Language;
use h1cms\model\Node;
use h1cms\model\NodeCategory;
use h1cms\model\NodeLanguage;
use Lg\Component\Categories;
use Lg\Component\Paginator;
use Lg\Http\Input;
use Lg\View\View;

// +----------------------------------------------------------------------
// | 模块名称: 内容管理
// +----------------------------------------------------------------------
//初始化
function initialize() {
	screen()->addBreadcrumb(__('文章列表'), url_for('/knowledgebase/knowledgebase.php'), '', false);
}

//文章列表
function indexAction() {
	$paginator = new Paginator(Input::get('page', 1), Input::get('limit', 20), Input::query());
	$filter['offset'] = $paginator->getOffset();
	$filter['pagesize'] = $paginator->getLimit();
	$total_rows = Node::count(); //總行數
	$node_list = Node::getAll($filter); //获取內容列表
	$paginator->setTotal($total_rows); //设置分页组件总行数
	screen()->setTitle(__('文章管理'), __('')); //设置页面标题
	return View::make('node', [
		'node_list' => $node_list,
		'total_rows' => $total_rows,
		'paginator' => $paginator,
	]);
}

//添加文章
function addAction() {
	if (Input::isMethod('post')) {
		return create_node();
	}
	$category = new Categories();
	$categories = $category->getCategory(['depth' => 1]);
	screen()->addBreadcrumb(__('添加文章'), '', '', false);
	screen()->setTitle(__('添加文章'), __('文章管理'));
	return View::make('node-form', [
		'categories' => $categories,
		'languages' => Language::getAllAvailables(),
	]);
}

//编辑分类
function editAction() {
	$node_id = intval(Input::get('id'));
	if (Input::isMethod('post')) {
		return update_node();
	}
	$category = new Categories;
	$categories = $category->getCategory(['depth' => 1]);
	screen()->setTitle(__('修改文章'), __('文章管理'));
	return View::make('node-form',
		[
			'categories' => $categories,
			'node' => Node::findById($node_id),
			'node_languages' => NodeLanguage::getAll($node_id),
			'node_category' => NodeCategory::getCategoryIds($node_id),
			'languages' => Language::getAllAvailables(),
		]);
}

//删除分类
function deleteAction() {
	return delete_node();
}
